package yxy.game.pm2.module.arena;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.function.Function;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.player.FunctionHandle;

import java.util.ArrayList;
import java.util.LinkedList;

public class ArenaPlayerModule extends PlayerModule {
	// bean===========================================================================
	private int times;// 挑战次数
	private int weektimes;// 周挑战次数
	private DateTime lastTime;
	private ArrayList<String> oppo;// 对手
	private LinkedList<String> fighted;// 交手过的对手

	private boolean skip;// 跳过战斗

	private ArrayList<String> weekbox = new ArrayList<>();// 已经领取的周宝箱

	private int combo;// 连胜

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public DateTime getLastTime() {
		return lastTime;
	}

	public void setLastTime(DateTime lastTime) {
		this.lastTime = lastTime;
	}

	public int getWeektimes() {
		return weektimes;
	}

	public void setWeektimes(int weektimes) {
		this.weektimes = weektimes;
	}

	public ArrayList<String> getOppo() {
		return oppo;
	}

	public void setOppo(ArrayList<String> oppo) {
		this.oppo = oppo;
	}

	public LinkedList<String> getFighted() {
		return fighted;
	}

	public void setFighted(LinkedList<String> fighted) {
		this.fighted = fighted;
	}

	public ArrayList<String> getWeekbox() {
		return weekbox;
	}

	public void setWeekbox(ArrayList<String> weekbox) {
		this.weekbox = weekbox;
	}

	public int getCombo() {
		return combo;
	}

	public void setCombo(int combo) {
		this.combo = combo;
	}

	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	// func===========================================================================

	/** 刷新每日的状态 */
	public void update() {
		try {
			if (tryLock()) {
				DateTime now = DateTime.now();

				if (getLastTime() == null) {
					setLastTime(now);
					set();
				}
				if (!new LocalDate(now).equals(new LocalDate(getLastTime()))) {
					setTimes(0);
					if (now.getWeekOfWeekyear() != getLastTime().getWeekOfWeekyear() || now.getWeekyear() != getLastTime().getWeekyear()) {
						setWeektimes(0);
						weekbox.clear();
						setCombo(0);
					}
					setLastTime(now);
					this.set();
				}
			}
		} catch (Exception e) {
		} finally {
			unlock();
		}
	}

	public int times() {
		int freeTimes = fomula.竞技场每日免费次数挑战.intValue();
		return freeTimes - getTimes();
	}

	// redis==========================================================================

	static public String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:ARENA", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ArenaPlayerModule get(String playerCode) {
		try {
			ArenaPlayerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ArenaPlayerModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				boolean open = new FunctionHandle().check(Function.竞技场, playerCode);
				if (!open) {
					return null;
				}
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private ArenaPlayerModule create(String playerCode) {
		ArenaPlayerModule module = new ArenaPlayerModule();
		module.setPlayerCode(playerCode);
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ArenaPlayerModule arenaPlayerModule = ArenaPlayerModule.getv(key(playerCode));
				MySQLCacheDAO.setV(key, arenaPlayerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
